﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MPEGnet
{
    public class PerfDebugger
    {
        Dictionary<string, TimeSpan> _dict = new Dictionary<string, TimeSpan>();

        public Dictionary<string, TimeSpan> TagSpans
        {
            get
            {
                return _dict;
            }
        }

        internal PerfTrace Trace(string tag)
        {
            return new PerfTrace(this, tag);
        }

        internal void EndTrace(string tag, TimeSpan span)
        {
            if (_dict.ContainsKey(tag))
                _dict[tag] += span;
            else
                _dict[tag] = span;
        }
    }

    internal struct PerfTrace : IDisposable
    {
        PerfDebugger _dbg;
        string _tag;
        DateTime _startTime;

        public PerfTrace(PerfDebugger dbg, string tag)
        {
            _dbg = dbg;
            _tag = tag;
            _startTime = DateTime.Now;
        }

        public void Dispose()
        {
            _dbg.EndTrace(_tag, DateTime.Now - _startTime);
        }
    }
}
